#include "specdesignmodel.h"
#include "earthquake_global.h"

SpecDesignModel::SpecDesignModel(SpecDesignL* ptr_sp_list, QObject *parent)
    : QAbstractTableModel(parent)
{
    m_sp_list = ptr_sp_list;
}

// Obbligatorio per modello sola lettura
int SpecDesignModel::rowCount(const QModelIndex &parent) const
{
    return(m_sp_list->sizeOf());
}

int SpecDesignModel::columnCount(const QModelIndex &parent) const
{
    return 6;
}

QVariant SpecDesignModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
         return QVariant();

    if (index.row() < 0)
         return QVariant();

    if (index.row() >= m_sp_list->sizeOf())
         return QVariant();

    if (index.column() < 0)
         return QVariant();

        if (index.column() >= 6)
         return QVariant();

    SpecDesign sp;
    sp = m_sp_list->getAt(index.row());
    if (role == Qt::DisplayRole)
    {
        QString qstr;
        switch (index.column())
        {
            case 0: // IDsp
                return QVariant(qstr.fromStdString(sp.getKeyStr()));
            case 1: // ductility factor
                if (sp.getTpSpecDesign().getFlag()!=TpSpecDesign::SLS)
                    return QVariant(sp.getDuctility().getValue());
                else
                    return QVariant("---");
            case 2: // dumping
                if (sp.getTpSpecDesign().getFlag()==TpSpecDesign::SLS)
                    return QVariant(sp.getDamping().getValue());
                else
                    return QVariant("---");
            case 3: // IDse
                return QVariant(qstr.fromStdString(sp.getRefFrom(EXA_TAB_SPECPARAM).getKeyStr()));
            case 4: // Descr
                return QVariant(qstr.fromStdString(sp.getDescr()));
            case 5: // Tipo
                return QVariant(qstr.fromStdString(sp.getTpSpecDesign().getFlagStr()));
        }
        return QVariant();
    }
    else if (role == Qt::UserRole)
    {
        return QVariant::fromValue(sp);
    }
        if (index.column() >= 0 && index.column() <=6 && role == Qt::TextAlignmentRole)
        return QVariant(Qt::AlignCenter|Qt::AlignVCenter);
        else return QVariant();
}

QVariant SpecDesignModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal)
       switch (section)
       {
           case 0:
               return tr("IDsp");
           case 1:
               return tr("Ductility Factor");
           case 2:
               return tr("Damping Factor");
           case 3:
               return tr("IDse");
           case 4:
               return tr("Descrizione");
           case 5:
               return tr("Tipo di spettro");
       }
    else
        return QString("Spettro di progetto n.%1").arg(section+1);
}

// Obbligatorio per modello resizeble. QModelIndex non serve nel caso di tabelle
bool SpecDesignModel::insertRows(int position, int rows, const QModelIndex &parent)
{
    Q_UNUSED(parent);
    beginInsertRows(QModelIndex(), position, position+rows-1);
    m_sp_list->insertAt(position, rows);
    endInsertRows();
    return true;
}

bool SpecDesignModel::removeRows(int position, int rows, const QModelIndex &parent)
{
    Q_UNUSED(parent);
    beginRemoveRows(QModelIndex(), position, position+rows-1);
    for (int row=0; row < rows; ++row) {
        m_sp_list->remove(position);
    }
    endRemoveRows();
    return true;
}

// Obbligatorio per modello editabile
bool SpecDesignModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole){
        int row = index.row();
        SpecDesign sp = value.value< SpecDesign >();
        m_sp_list->valueAt(row) = sp;
        QModelIndex i = this->index(row,6);
        emit(dataChanged(index,i));
    }
    else return false;
    return true;
}

void SpecDesignModel::updateWEDChange(void)
{
    // beginInsertRows();
    reset();
    emit( dataChanged(QModelIndex(),QModelIndex()) );
    // endInsertRows();
}
